var webp_converter = require("webp-converter")
const path = require("path")
const fs = require("fs")

let pngRoot = path.join(__dirname, 'doing');
let webpOut = path.join(__dirname, '../../assets/LevelAb/')

let levelResArray = [
    'A004', 'A005', 'A010', 'A011', 'A012',
    'A013', 'A014', 'A016', 'A018', 'A019',
    'A021', 'A022', 'A023', 'A026', 'A027',
    'A028', 'A029', 'A030', 'A031', 'A033',
    'A034', 'A036', 'A037', 'A038', 'A040',
    'A041', 'A044', 'A045', 'A047', 'A048',
    'A049', 'A050', 'A051', 'A053', 'A060',
    'A061', 'A062', 'A063', 'A064', 'A065',
    'A066', 'A067', 'A068', 'A069', 'A070',
    'A071', 'A072', 'A055', 'A056', 'ani001',
    'ani002', 'ani003',
    'food001', 'food002', 'food003', 'food004', 'food005',
    'food006', 'food007', 'food008', 'food009', 'food010',
    'food011', 'food012', 'food013', 'food014', 'food015',
    'food016', 'food017', 'food018', 'food019', 'm063',
    'm080', 'm118', 'm169', 'm267'
]

let handled = []
let dropped = []
/**是否只处理近一周的 */
let is_handle_recent_week = true
// 获取两个日期之间的天数差
function daysBetween(date1, date2) {
    const oneDayMs = 1000 * 60 * 60 * 24;
    const diffMs = Math.abs(date2 - date1);
    return Math.floor(diffMs / oneDayMs);
}

function handDir(dirPath) {
    let allLevelNames = []
    let readDir = fs.readdirSync(dirPath);
    readDir.forEach((file, idx) => {
        let fullPath = path.join(dirPath, file)
        var info = fs.statSync(fullPath)
        const modifiedTime = info.mtime;
        const today = new Date();
        const daysDiff = daysBetween(modifiedTime, today);
        if(is_handle_recent_week && daysDiff > 7) {
            return
        }
        console.log(`文件最后修改时间是：${modifiedTime}`);
        console.log(`距离今天已有 ${daysDiff} 天`);
        if (info.isDirectory()) {
            handDir(fullPath)
        } else {
            let fileType = path.extname(fullPath);
            console.log(`handle ${fullPath} type:${fileType}`)
            let targetPath = fullPath.replace(pngRoot, webpOut)
            if (fileType === '.png' || fileType === '.jpg') {
                let outFile = targetPath.replace(fileType, '.png') //将webp格式后缀改为png，微信小游戏不支持上传webp格式
                webp_converter.cwebp(fullPath, outFile, '-q 90', (status) => {

                })
            } else {
                if (fileType === ".json") {
                    let filename = path.basename(fullPath, path.extname(fullPath))
                    allLevelNames.push(filename)
                }else{
                    //不拷json了
                    fs.copyFile(fullPath, targetPath, () => { })
                }
            }
        }
    })
    const quotedStrings = allLevelNames.map(item => `${item}`);

    console.log("全部关卡: ", quotedStrings)
}

function checkNeedDrop(dirPath) {
    let readDir = fs.readdirSync(dirPath);
    readDir.forEach((file, idx) => {
        let fullPath = path.join(dirPath, file)
        var info = fs.statSync(fullPath)
        if (info.isDirectory()) {
            handDir(fullPath)
        } else {
            let fileType = path.extname(fullPath);
            if (fileType == ".json") {
                let filename = path.basename(fullPath, path.extname(fullPath))
                let findIndex = levelResArray.indexOf(filename)
                // console.log(`处理资源:${fullPath} ${filename} ${findIndex}`)
                if (findIndex == -1) {
                    dropped.push(filename)
                } else {
                    levelResArray.splice(findIndex, 1)
                }
                // console.log(`${levelResArray.join(",")}`)
            }
        }
    })
    console.log(`应该删除的关卡:${dropped.join(",")}`)
    console.log(`资源缺失的关卡:${levelResArray.join(",")}`)
}

//checkNeedDrop(pngRoot)
handDir(pngRoot)